<?php
/**
 * Google image maps generator
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * API required? @SEE: https://console.developers.google.com/
 *
 * */


Class Yapary_Webservice_Google_GoogleStaticMaps
{
	private $filePathMaps			= 'https://maps.googleapis.com/maps/api/staticmap';
	private $filePathMarker			= 'http://chart.apis.google.com/chart';

	private $arrMarker				= array();

	private $addressCenter			= NULL;
	private $zoom					= NULL;
	private $mapType				= NULL;


	const	MAP_TYPE_ROADMAP		= 'roadmap';
	const	MAP_TYPE_SATELLITE		= 'sattelite';
	const	MAP_TYPE_HYBRID			= 'hybrid';
	const	MAP_TYPE_TERRAIN		= 'terrain';


	function __construct () {
	}


	public function setMapType ($_mapType) {
		$this->mapType				= $_mapType;
	}


	public function setZoom ($_factor = 10) {
		if (!is_numeric($_factor)) {
			return false;
		}

		$this->zoom					= $_factor;
	}


	/**
	 * Center a map with the specified data. If not use, the map will center automaticly
	 * */
	public function setCenter ($_address) {
		$this->addressCenter		= $_address;
	}


	public function setMarker ($_address, $_color = 'green', $_labelChar = 'A') {
		$this->arrMarker[]			= array(
										'address'	=> $_address,
										'color'		=> $_color,
										'label'		=> $_labelChar
									);
	}


	public function getUrl ($_width = '640', $_height = '640') {
		$url				= '';

		$url				.= '?size='.$_width.'x'.$_height;

		if ($this->zoom !== NULL) {
			$url			.= '&zoom='.$this->zoom;
		}
		if ($this->mapType !== NULL) {
			$url			.= '&maptype='.$this->mapType;
		}
		if ($this->addressCenter !== NULL) {
			$url			.= '&center='.rawurlencode($this->addressCenter);
		}

		if (count($this->arrMarker)) {
			foreach ($this->arrMarker as $key => $arrMarker) {
				$address		= $arrMarker['address'];
				$color			= $arrMarker['color'];
				$label			= $arrMarker['label'];

				$url			.= '&markers=color:'.$color.'|label:'.$label.'|'.str_replace(' ', '+', $address);
			}
		}

		return $this->filePathMaps.$url;
	}


	public function display ($_width = '640', $_height = '640', $_name = 'Google Maps') {
		return '<img src="'.$this->getUrl($_width, $_height).'" alt="'.$_name.'" />';
	}


}